/*
 * @Author: hyman
 * @Date: 2021-12-22 08:54:44
 * @LastEditors: hyman
 * @LastEditTime: 2021-12-31 11:42:07
 * @Description: 请填写简介
 */
import { createApp } from 'vue'
import App from './App.vue'

//加载接口
import api from './api/index.js'

//引入路由对象
import router from './routers/index'

//引入UI框架
import Vant from 'vant';
import 'vant/lib/index.css';

//引入Cookies
import VueCookies from 'vue3-cookies'

//vuex
import store from './store/index'

//挂载
const app = createApp(App)
    .use(VueCookies)
    .use(Vant) //UI
    .use(router) //挂载路由
    .use(api) //挂载接口   === install
    .use(store)
    .mount('#app')


//添加路由守卫做判断是否有无登录
router.beforeEach(async(to, from, next) => {

    if(to.meta.auth){
        // //需要登录
        
        //判断是否有无cookie 
        var LoginUser = app.$cookies.get('LoginUser');

        //没有登录就跳转到登录界面
        if(!LoginUser)
        {
            next('/user/base/login')
            return
        }

        //获取id
        var userid = LoginUser.id ? LoginUser.id : 0;
     

        //接口请求
        var result = await api.check({userid:userid})

        if(result.code == 0)
        {
            //验证失败
            app.$cookies.remove('LoginUser')

            //跳转了
            next('/user/base/login')
            return
        }else
        {
            //验证成功
            //覆盖cookie
            app.$cookies.set('LoginUser', result.data)
            
            //让他去到该去的路由
            next()
        }


    }else
    {
        //不需要登录,直接跳转路由
        next()
    }

})